View Javadoc

1   // ObjectLayoutMethods.java, created Mon Dec 16 22:24:22 2002 by mcmartin
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Allocator;
5   
6   import joeq.Memory.HeapAddress;
7   import joeq.Runtime.Unsafe;
8   
9   /*** This interface contains utility functions for the joeq object layout.
10   *  You can play with these constants to experiment with different object layouts.
11   *
12   * @author  John Whaley <jwhaley@alum.mit.edu>
13   * @version $Id: ObjectLayoutMethods.java 1456 2004-03-09 22:01:46Z jwhaley $
14   */
15  public abstract class ObjectLayoutMethods {
16      public static Object initializeObject(HeapAddress addr, Object vtable, int size) {
17          addr = (HeapAddress) addr.offset(ObjectLayout.OBJ_HEADER_SIZE);
18          addr.offset(ObjectLayout.VTABLE_OFFSET).poke(HeapAddress.addressOf(vtable));
19          return addr.asObject();
20      }
21      
22      public static Object initializeArray(HeapAddress addr, Object vtable, int length, int size) {
23          addr = (HeapAddress) addr.offset(ObjectLayout.ARRAY_HEADER_SIZE);
24          addr.offset(ObjectLayout.ARRAY_LENGTH_OFFSET).poke4(length);
25          addr.offset(ObjectLayout.VTABLE_OFFSET).poke(HeapAddress.addressOf(vtable));
26          return addr.asObject();
27      }
28      
29      public static int getArrayLength(Object obj) {
30          HeapAddress addr = HeapAddress.addressOf(obj);
31          return addr.offset(ObjectLayout.ARRAY_LENGTH_OFFSET).peek4();
32      }
33      
34      public static void setArrayLength(Object obj, int newLength) {
35          HeapAddress addr = HeapAddress.addressOf(obj);
36          addr.offset(ObjectLayout.ARRAY_LENGTH_OFFSET).poke4(newLength);
37      }
38      
39      public static Object getVTable(Object obj) {
40          HeapAddress addr = HeapAddress.addressOf(obj);
41          return ((HeapAddress) addr.offset(ObjectLayout.VTABLE_OFFSET).peek()).asObject();
42      }
43      
44      public static boolean testAndMark(Object obj, int markValue) {
45          HeapAddress addr = (HeapAddress) HeapAddress.addressOf(obj).offset(ObjectLayout.STATUS_WORD_OFFSET);
46          for (;;) {
47              int oldValue = addr.peek4();
48              int newValue = (oldValue & ~ObjectLayout.GC_BIT) | markValue;
49              if (oldValue == newValue)
50                  return false;
51              addr.atomicCas4(oldValue, newValue);
52              if (Unsafe.isEQ())
53                  break;
54          }
55          return true;
56      }
57      
58      public static boolean testMarkBit(Object obj, int markValue) {
59          HeapAddress addr = (HeapAddress) HeapAddress.addressOf(obj).offset(ObjectLayout.STATUS_WORD_OFFSET);
60          int value = addr.peek4();
61          return (value & ObjectLayout.GC_BIT) == markValue;
62      }
63      
64      public static void writeMarkBit(Object obj, int markValue) {
65          HeapAddress addr = (HeapAddress) HeapAddress.addressOf(obj).offset(ObjectLayout.STATUS_WORD_OFFSET);
66          int oldValue = addr.peek4();
67          int newValue = (oldValue & ~ObjectLayout.GC_BIT) | markValue;
68          addr.poke4(newValue);
69      }
70  }